import scala.io.StdIn

class SecondJob  {

  private var bot: Boot = _
  private var count: Int = 0
  private var botScore, playerScore: Int = 0
  private val FINGERS = Array("剪刀", "石头", "布")

  //选择对手
  private def ChoiceBoot() = {
    var flag = true
    while (flag){
      println(s"请选择对战角色：${Boot.peopleList}")

      var message = StdIn.readLine()
      val bool = selectBoot(message.toInt)
      if(bool){
        println("要开始吗？y/n")
        message = StdIn.readLine()
        if(message == "y") {
          flag = false
        }
      }
    }
  }

  private def playGame(): Unit = {
    var flag = true
    while (flag){
      println("请出拳！1.剪刀 2.石头 3.布")
      var message = StdIn.readLine()
      val playerFinger = message match {
        case x if x.toInt<1 || x.toInt>3 =>{
          println("输入不符合规范，默认出布")
          "布"
        }
        case  _=>FINGERS(message.toInt - 1)
      }
      println(s"你出拳: ${playerFinger}")
      val botFinger = FINGERS(bot.randomFocus - 1)
      println(s"${bot.name}出拳: $botFinger")

      checkFinger(playerFinger, botFinger) match {
        case "win" => {
          println("恭喜，你赢了！")
          playerScore += 2
        }
        case "lose" => {
          println("哦呦，你输了！")
          botScore += 2
        }
        case _=> {
          println("平局，下次继续努力。")
          playerScore += 1
          botScore += 1
        }
      }
      count += 1
      println("是否开启下一轮? y/n")
      message = StdIn.readLine()
      if (message == "n") {
        flag = false
      }
    }
  }

  private def checkFinger(playerFinger: String, botFinger: String) = {
    (playerFinger, botFinger) match {
      case (x,y) if x == y => "tied"
      case ("剪刀", "石头") |
           ("石头", "布") |
           ("布", "剪刀") => "lose"
      case _ => "win"
    }
  }

  def selectBoot(index:Int): Boolean ={
    try{
      bot = Boot(index)
    }catch {
      case _=> {
        println("输入无效")
        return false
      }
    }
    println(s"你选择了与${bot.name}对战")
    true
  }

  private def playSummary() = {
    println("退出游戏！\n----------------------------")
    println(s"${bot.name} VS 游客")
    println(s"对战次数: ${count}次，${bot.name}得分${botScore}，你得分${playerScore}")
  }

}

object SecondJob{
  def main(args: Array[String]): Unit = {
    val job = new SecondJob()

    start()

    def start() = {
      job.ChoiceBoot()
      job.playGame()
      job.playSummary()
    }
  }
}

